package com.kalix.mooc.reply.biz;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.framework.core.util.SerializeUtil;
import com.kalix.mooc.reply.api.biz.IMessageReplyBeanService;
import com.kalix.mooc.reply.api.dao.IMessageReplyBeanDao;
import com.kalix.mooc.reply.api.dto.MessageReplyTreeDTO;
import com.kalix.mooc.reply.entities.MessageReplyBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 *
 * @Description: 留言回复实现类
 * @Author: yyp
 * @CreateDate: 2019/5/29$ 10:49$
 * @Version:
 */
public class MessageReplyBeanServiceImpl extends ShiroGenericBizServiceImpl<IMessageReplyBeanDao, MessageReplyBean> implements IMessageReplyBeanService {
    /**

     * @Description: 根据id删除回复
     * @Author: yyp
     * @CreateDate: 2019/5/29
     * @Version:
     */
    @Override
    public void deleteAllByPostid(long id) {
        dao.deleteAllByPostid(id);
    }
    /**
     * @Description: 回复“树”
     * @Author: yyp
     * @CreateDate: 2019/5/29
     * @Version:
     */
    @Override
    public MessageReplyTreeDTO getMessageReplyByPostId(long messageId, String jsonStr) {
        Map<String, Object> jsonMap = SerializeUtil.jsonToMap(jsonStr);
        String userName = (String)jsonMap.get("%userName%");
        MessageReplyTreeDTO root = new MessageReplyTreeDTO();
        // ReplyTreeDTO root = new ReplyTreeDTO();
        root.setId(-1L);
        List<MessageReplyBean> beans = new ArrayList<>();
        String sql = "select r from MessageReplyBean r where 1=1";
        if (userName != null && !userName.isEmpty())  {
            sql += " and r.userName like '%"+userName+"%'";
        }
        if(messageId != -1){
            sql += " and r.messageId =" + messageId;
        }
        sql += " order by r.creationDate desc";
        beans = dao.find(sql);

        if(beans != null && beans.size() > 0){
            List<MessageReplyBean> rootElements = getRootElements(beans);
            if(rootElements != null && rootElements.size() > 0){
                for(MessageReplyBean rootElement:rootElements){
                    Mapper mapper = DozerBeanMapperBuilder.buildDefault();
                    MessageReplyTreeDTO messageReplyTreeDTO = mapper.map(rootElement,MessageReplyTreeDTO.class);
                    messageReplyTreeDTO.setLeaf(rootElement.getIsLeaf() == 0 ? false : true);
                    messageReplyTreeDTO.setParentName("根留言");
                    getChilden(messageReplyTreeDTO, beans, mapper);
                    root.getChildren().add(messageReplyTreeDTO);
                }
            }
        }
        return root;
    }

    /**
     * 递归函数加载子节点
     * @param root
     * @param elements
     */
    private void getChilden(MessageReplyTreeDTO root, List<MessageReplyBean> elements, Mapper mapper){
        List<MessageReplyTreeDTO> children = new ArrayList<MessageReplyTreeDTO>();
        for(MessageReplyBean messageReplyBean : elements){
            if(root.getId() != -1 && (root.getId() == messageReplyBean.getParentId())){
                MessageReplyTreeDTO messageReplyTreeDTO = mapper.map(messageReplyBean,MessageReplyTreeDTO.class);
                messageReplyTreeDTO.setLeaf(messageReplyBean.getIsLeaf() == 0 ? false : true);
                messageReplyTreeDTO.setParentName(root.getUserName());
                children.add(messageReplyTreeDTO);
                if(messageReplyBean.getIsLeaf() == 0){
                    getChilden(messageReplyTreeDTO, elements ,mapper);
                }
            }
        }
        root.setChildren(children);
    }

    /**
     * 获得所有根节点
     * @param elements
     * @return
     */

    private List<MessageReplyBean> getRootElements(List<MessageReplyBean> elements) {
        List<MessageReplyBean> roots=new ArrayList<MessageReplyBean>();
        for (MessageReplyBean element : elements) {
            if (element.getParentId() == -1) {
                roots.add(element);
            }
        }
        return roots;
    }
}
